package com.txx.app.main.commom.mvvm;

import android.app.Dialog;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.qihoo360.replugin.RePlugin;
import com.tendcloud.tenddata.TCAgent;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.txx.app.main.App;
import com.txx.app.main.R;
import com.txx.app.main.commom.bean.LogOut;
import com.txx.app.main.commom.ui.ActivityStack;
import com.txx.app.main.commom.ui.MyDialog;
import com.txx.app.main.commom.ui.MyToast;
import com.txx.app.main.utils.UserUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by renshiqian on 2018/10/30.
 */
public class MVVMBaseActivity extends RxAppCompatActivity {

    private Dialog dialog;
    private App app;
    private MyDialog myDialog;
    private MyDialog meDialog;
    private DialogListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        ActivityStack.getInstance ().addActivity (this);
        EventBus.getDefault ().register (this);
        createView ();
        initUserInfo ();
        myDialog = new MyDialog (this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void popupNetWorkWindow(LogOut state) {
        if (state.isLogout ()) {
            myDialog.showDialog (R.layout.dialog_verify);
            TextView tv_msg1 = (TextView) myDialog.findViewById (R.id.tv_warn_msg);
            tv_msg1.setText (state.getDesc ());
            myDialog.findViewById (R.id.tv_warn_true).setOnClickListener (new View.OnClickListener () {
                @Override
                public void onClick(View v) {
                    //app.logout();
                    myDialog.dismiss ();
                }
            });

        }
    }

    /**
     * 初始化宿主的用户信息
     */
    private void initUserInfo() {
        if (RePlugin.getHostContext () != null) {
            UserUtils.initUserInfo ();
        }

    }


    /**
     * 不依赖系统字体大小变化
     *
     * @return
     */
    @Override
    public Resources getResources() {
        boolean isPad= com.txx.app.main.utils.DeviceType.isPad2 ( super.getResources().getConfiguration ());
        if(isPad){
            return super.getResources();
        }
        Resources res = super.getResources ();
        Configuration config = new Configuration ();
        config.setToDefaults ();
        res.updateConfiguration (config, res.getDisplayMetrics ());
        return res;
    }


    public void dismissProgressDialog() {
        if (dialog != null && !isFinishing ()) {
            dialog.dismiss ();
        }
    }

    private void createView() {
        View layout = View.inflate (this, R.layout.dialog_loading_bg, null);
        dialog = new Dialog (this, R.style.dialog);
        dialog.setCancelable (true);
        dialog.setContentView (layout);
    }

    private void createView2() {
        View layout = View.inflate (this, R.layout.dialog_loading_bg2, null);
        dialog = new Dialog (this, R.style.dialog);
        dialog.setCancelable (true);
        dialog.setContentView (layout);
    }

    public void showProgressDialog() {
        if (dialog != null && !isFinishing ()) {
            dialog.show ();
        }
    }

    public void showProgressDialog(boolean cancelable) {
        if (dialog != null && !isFinishing ()) {
            dialog.setCancelable (false);
            dialog.show ();
        }
    }

    @Override
    protected void onResume() {
        super.onResume ();
        TCAgent.onResume (this);
    }

    @Override
    protected void onPause() {
        super.onPause ();
        TCAgent.onPause (this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy ();
        dismissProgressDialog ();
        EventBus.getDefault ().unregister (this);
        ActivityStack.getInstance ().finishOwnerActivity (this);
    }

    public void showError(String msg) {
        MyToast.showShort (msg);
    }

    public void showErrorDialog(String msg) {
        if (meDialog == null) {
            meDialog = new MyDialog (this);
            return;
        }
        meDialog.showDialog (R.layout.dialog_verify);
        TextView tv_msg = (TextView) meDialog.findViewById (R.id.tv_warn_msg);
        tv_msg.setText (msg);
        meDialog.findViewById (R.id.tv_warn_true).setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                meDialog.dismiss ();
            }
        });
    }

//    public void showDialogYesOrNo(String msg, final boolean isFinish, final DialogListener listener) {
//        this.listener=listener;
//        if (meDialog == null) {
//            meDialog = new MyDialog(this);
//        }
//        meDialog.showDialog(R.layout.basemodule_dialog_sure_and_cancel_type_one);
//        TextView tv_msg = (TextView) meDialog.findViewById(R.id.tv_content);
//        tv_msg.setText(msg);
//        meDialog.findViewById(R.id.dialog_exit_true).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (isFinish) {
//                    finish();
//                }
//                if(listener!=null){
//                    listener.onClickListenerYes();
//                }
//                meDialog.dismiss();
//            }
//        });
//        meDialog.findViewById(R.id.dialog_exit_false).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(listener!=null){
//                    listener.onClickListenerNo();
//                }
//                meDialog.dismiss();
//            }
//        });
//    }
}
